import FormatUtil from './FormatUtil'

const ValidateUtil = {
  // 判空
  checkEmpty: function (string) {
    return (
      typeof string === 'undefined' ||
      string === null ||
      string === '' ||
      string === 0 ||
      (typeof string === 'number' && !parseFloat(string)) ||
      JSON.stringify(string) === '{}'
    )
  },

  // 校验手机号
  checkPhone: function (phone) {
    return /^1\d{10}$/.test(FormatUtil.formatSpace(phone))
  },
  // 校验身份证号
  checkIdCardNo: function (idCardNo) {
    return /^([1-9]\d{5}(19|([20]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9X])$/.test(
      idCardNo
    )
  },

  // 检验是否是数字字符串
  checkNumber: function (str) {
    return /^[0-9]*$/.test(str)
  },

  // 校验密码
  checkPassword: function (password) {
    return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(password)
  },
  // 校验验证码
  checkCaptcha: function (captcah) {
    return /^\d{6}\b/.test(captcah)
  },

  // 校验图形验证码
  checkGraphCaptcha: function (captcah) {
    return /^\d{4}\b/.test(captcah)
  },

  // 车架号
  checkVIN: function (vin) {
    if (vin && (vin.length === 17 || vin.length === 18)) {
      return true
    }
    return false
  },

  // 校验车牌号
  checkPlateNo: function (plateNo) {
    const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/
    const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
    if (plateNo.length === 7) {
      // 普通
      return creg.test(plateNo)
    } else if (plateNo.length === 8) {
      // 新能源
      return xreg.test(plateNo)
    } else {
      return false
    }
  },

  // 校验中文名字
  checkChineseName: (string) => {
    return /^[\u4E00-\u9FA5]{1,16}(?:·[\u4E00-\u9FA5]{1,16})*$/.test(string)
  },
  // 校验银行卡
  checkBankCardNo: function (bankNo) {
    const lastNum = bankNo.substr(bankNo.length - 1, 1)

    const first15Num = bankNo.substr(0, bankNo.length - 1)
    const newArr = []
    for (let i = first15Num.length - 1; i > -1; i--) {
      newArr.push(first15Num.substr(i, 1))
    }
    const arrJiShu = []
    const arrJiShu2 = []

    const arrOuShu = []
    for (let j = 0; j < newArr.length; j++) {
      if ((j + 1) % 2 === 1) {
        if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2)
        else arrJiShu2.push(parseInt(newArr[j]) * 2)
      } else arrOuShu.push(newArr[j]) // 偶数位
    }

    const jishuChild1 = []
    const jishuChild2 = []
    for (let h = 0; h < arrJiShu2.length; h++) {
      jishuChild1.push(parseInt(arrJiShu2[h]) % 10)
      jishuChild2.push(parseInt(arrJiShu2[h]) / 10)
    }

    let sumJiShu = 0
    let sumOuShu = 0
    let sumJiShuChild1 = 0
    let sumJiShuChild2 = 0
    let sumTotal = 0
    for (let m = 0; m < arrJiShu.length; m++) {
      sumJiShu = sumJiShu + parseInt(arrJiShu[m])
    }

    for (let n = 0; n < arrOuShu.length; n++) {
      sumOuShu = sumOuShu + parseInt(arrOuShu[n])
    }

    for (let p = 0; p < jishuChild1.length; p++) {
      sumJiShuChild1 = sumJiShuChild1 + parseInt(jishuChild1[p])
      sumJiShuChild2 = sumJiShuChild2 + parseInt(jishuChild2[p])
    }

    sumTotal =
      parseInt(sumJiShu) +
      parseInt(sumOuShu) +
      parseInt(sumJiShuChild1) +
      parseInt(sumJiShuChild2)

    const k = parseInt(sumTotal) % 10 === 0 ? 10 : parseInt(sumTotal) % 10
    const luhm = 10 - k
    return lastNum === luhm
  },

  // string判断是否有值
  isEmptyString: function (string) {
    if (string === null || string === '') {
      return true
    }
    return false
  },

  isChineseCharacter: function (substring) {
    const re = /^[\u4E00-\u9FA5]+$/
    if (re.test(substring)) {
      return true
    } else {
      return false
    }
  },
  // 校验所有输入域是否含有特殊符号（.@可以输入）
  isSpecialCharacter: function (substring) {
    const pattern = new RegExp("[·.@`~!#$^&*()=|{}':;',\\[\\]<>/?~！#￥……&*（）&dash;—|{}【】‘；：”“'。，、？]")
    if (pattern.test(substring)) {
      return true
    }
    return false
  },

  isEmojiCharacter: function (substring) {
    for (let i = 0; i < substring.length; i++) {
      const hs = substring.charCodeAt(i)
      if (0xd800 <= hs && hs <= 0xdbff) {
        if (substring.length > 1) {
          const ls = substring.charCodeAt(i + 1)
          const uc = (hs - 0xd800) * 0x400 + (ls - 0xdc00) + 0x10000
          if (0x1d000 <= uc && uc <= 0x1f77f) {
            return true
          }
        }
      } else if (substring.length > 1) {
        const ls = substring.charCodeAt(i + 1)
        if (ls === 0x20e3) {
          return true
        }
      } else {
        if (0x2100 <= hs && hs <= 0x27ff) {
          return true
        } else if (0x2b05 <= hs && hs <= 0x2b07) {
          return true
        } else if (0x2934 <= hs && hs <= 0x2935) {
          return true
        } else if (0x3297 <= hs && hs <= 0x3299) {
          return true
        } else if (
          hs === 0xa9 ||
          hs === 0xae ||
          hs === 0x303d ||
          hs === 0x3030 ||
          hs === 0x2b55 ||
          hs === 0x2b1c ||
          hs === 0x2b1b ||
          hs === 0x2b50
        ) {
          return true
        }
      }
    }
  },

  // 校验邮箱格式
  checkEmail: function (str) {
    return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,3}$/.test(str)
  },

  // 只能输入两个小数
  checkAmount: function (str) {
    if (str.substr(0, 1) === '.') {
      return false
    }
    return /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/.test(str)
  },
}

export default ValidateUtil
